package javaTec.ThreadPrograming;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.builder.ToStringBuilder;

public class Count {

	private long index = 0;

	private Lock lock = new ReentrantLock();

	private AtomicLong atomicIndex = new AtomicLong();

	private LongAdder adderIndex = new LongAdder();

	public synchronized long incrBySynchronized() {
		return ++index;
	}

	public long incrByLock() {
		try {
			lock.lock();
			return ++index;
		} finally {
			lock.unlock();
		}

	}

	public long incrByAtomic() {
		return atomicIndex.incrementAndGet();
	}

	public long incrByLongAdder() {
		adderIndex.increment();
		return adderIndex.longValue();
	}


	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
